package com.keen.gzr.androidjs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    private CustomWebView mWebView;
    private Context mContext;
    private Button btnTest;
    private EditText etContent;
    private Button btnTestAddress;  //测试地址
    private Button btnAliYunAddress;//阿里云地址
    private String mUrl = "http://10.165.40.108:8080/ICloudLab/";

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        mWebView = (CustomWebView) findViewById(R.id.wv_main);
        etContent = (EditText) findViewById(R.id.et_edittext);
        
        btnTestAddress = (Button) findViewById(R.id.btn_test_address);
        btnAliYunAddress = (Button) findViewById(R.id.btn_yun_address);
        btnTestAddress.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				mUrl = "http://10.165.40.108:8080/ICloudLab/";
				mWebView.loadUrl(mUrl);
				Toast.makeText(mContext , "已经切换到测试地址" , Toast.LENGTH_SHORT).show();
			}
		});
        
        btnAliYunAddress.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				mUrl = "http://www.goldpac.cn/";
				mWebView.loadUrl(mUrl);
				Toast.makeText(mContext , "已经切换到阿里云" , Toast.LENGTH_SHORT).show();
			}
		});
        
        btnTest = (Button) findViewById(R.id.btn_test);
        btnTest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String strContent = etContent.getText().toString().trim();
				if (TextUtils.isEmpty(strContent)) {
					mWebView.loadUrl("javascript:funFromjs('您什么也没有输入')");
				} else {
					mWebView.loadUrl("javascript:funFromjs('" + strContent + "')");
				}
				
			}
		});
        
//        mWebView.loadUrl("http://www.goldpac.cn/");  //http://10.165.40.108:8080/ICloudLab/index/index.action
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.addJavascriptInterface(new TestJavascriptInterface() , "Android");
//        mWebView.loadUrl("file:///android_asset/test.html");
//        mWebView.loadUrl("http://10.165.40.108:8080/ICloudLab/index/index.action");
        mWebView.loadUrl(mUrl);
        
    }

    final class TestJavascriptInterface {

        public TestJavascriptInterface() {

        }

        @JavascriptInterface
        public String showToast(String string) {
            Toast.makeText(mContext , "接收到的数据------>" + string , Toast.LENGTH_LONG).show();
            Log.v("zr" , "接收到的数据------>" + string);
            return string;
        }
    }
}
